package com.vf.cloud.paas.app.master.api.itester.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.paas.app.master.api.itester.service.IITester;
import com.vf.cloud.paas.app.master.common.domain.ApiTest;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/itester")
public class ITestContentController {

	@Autowired
	private IITester itesterImpl;

	@RLog(value = "API版本列表", type = "user")
	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<ApiTest>> pages(HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "pid", required = false, defaultValue = "-1") String pid,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " from " + ApiTest.TABLE_NAME + "  a WHERE pid='" + pid + "' ";
		sqlExceptSelect += "  ORDER BY a.version desc ";
		return R.ok(ApiTest.dao.paginate(cur, limit, "select * ", sqlExceptSelect));
	}

	@RLog(value = "API列表", type = "user")
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<ApiTest>> list(HttpServletRequest request, @RequestBody ApiTest param) {
		return R.ok(itesterImpl.list(param));
	}

	@RLog(value = "保存api", type = "user")
	@TokenRequired
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ApiTest> save(@RequestBody ApiTest apiTest) {
		if (StrKit.isBlank(apiTest.getId())) {
			apiTest.setId(UuidUtil.getUUID());

			if (StrKit.equals("version", apiTest.getType())) {
				ApiTest version = ApiTest.dao.findFirst(
						"select * from " + ApiTest.TABLE_NAME + " where version=? and type='version' ",
						apiTest.getVersion());
				if (version != null) {
					return R.failed("版本号重复.");
				}
			}

			if (StrKit.isBlank(apiTest.getPid())) {
				apiTest.setPid("-1");
			}
			if (apiTest.save())
				return R.ok(apiTest);
		}

		if (StrKit.equals("version", apiTest.getType())) {
			ApiTest version = ApiTest.dao.findFirst(
					"select * from " + ApiTest.TABLE_NAME + " where  version=? and type='version' and id!=? ",
					apiTest.getVersion(), apiTest.getId());
			if (version != null) {
				return R.failed("版本号重复.");
			}
		}

		if (apiTest.update())
			return R.ok(apiTest);
		return R.failed();
	}

	@RLog(value = "删除API", type = "user")
	@TokenRequired
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ApiTest apiTest) {
		if (apiTest == null) {
			return R.failed("参数必填.");
		}
		if (ApiTest.dao.deleteById(apiTest.getId())) {
			itesterImpl.delete(apiTest.getId());
			return R.ok();
		}
		return R.failed();
	}

	@RLog(value = "获取API", type = "user")
	@TokenRequired
	@PostMapping(value = { "/get" }, produces = "application/json; charset=utf-8")
	public R<ApiTest> get(@RequestBody ApiTest param) throws Exception {
		ApiTest apiTest = ApiTest.dao.findById(param.getId());
		if (apiTest == null) {
			return R.failed();
		}
		return R.ok(apiTest);
	}

	@RLog(value = "复制API", type = "user")
	@TokenRequired
	@PostMapping(value = { "/copy" }, produces = "application/json; charset=utf-8")
	public R<ApiTest> copy(@RequestBody ApiTest param) throws Exception {
		ApiTest apiTest = itesterImpl.copy(param.getId());
		if (apiTest == null) {
			return R.failed();
		}
		return R.ok(apiTest);
	}
	
	
	@RLog(value = "根据版本号获取API列表", type = "user")
	@TokenRequired
	@RequestMapping(value = "/listByVersion", method = RequestMethod.POST)
	public R<List<ApiTest>> listByVersion(HttpServletRequest request, @RequestBody ApiTest param) {
		return R.ok(itesterImpl.listByVersion(param));
	}
}
